package com.daocao.hua.api.requestInfo;

import com.daocao.hua.api.Api;
import com.daocao.hua.api.bean.ApplyStatusBean;
import com.daocao.hua.api.bean.LoginBean;
import com.daocao.hua.api.callback.ApplyStatusContract;
import com.daocao.hua.api.callback.LoginContract;
import com.daocao.hua.util.Network;
import com.daocao.hua.util.RxLifeUtil;
import com.daocao.hua.util.ToastUtil;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.themes.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;
import cn.sharesdk.tencent.qq.QQ;
import cn.sharesdk.tencent.qzone.QZone;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;

/**
 * Created by Administrator on 2017/5/14.
 */

public class NewApplyForRequest extends BaseRequest<ApplyStatusContract.View> implements ApplyStatusContract.Presenter<ApplyStatusContract.View>, PlatformActionListener {

    public static NewApplyForRequest create() {
        return new NewApplyForRequest();
    }

    public void share(int platform, String desc, String image, String title, String url) {
        String pm = null;
        switch (platform) {
            case 1:
                pm = WechatMoments.NAME;
                break;
            case 2:
                pm = Wechat.NAME;
                break;
            case 3:
                pm = QQ.NAME;
                break;
            case 4:
                pm = SinaWeibo.NAME;
                break;
        }
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
        oks.setPlatform(pm);
        // title标题，印象笔记、邮箱、信息、微信、人人网、QQ和QQ空间使用
        oks.setTitle(title);
        // titleUrl是标题的网络链接，仅在Linked-in,QQ和QQ空间使用
        oks.setTitleUrl(url);
        // text是分享文本，所有平台都需要这个字段
        oks.setText(desc);
        //分享网络图片，新浪微博分享网络图片需要通过审核后申请高级写入接口，否则请注释掉测试新浪微博
        oks.setImageUrl(image);
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        //oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setComment(desc);
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite(title);
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl(url);
        oks.setUrl(url);
        oks.show(BaseContext);

    }

    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        ToastUtil.makeText(BaseContext, "分享成功").show();
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        ToastUtil.makeText(BaseContext, "分享失败，原因：" + throwable.toString()).show();
    }

    @Override
    public void onCancel(Platform platform, int i) {

    }

    @Override
    public void getApplyFor(String deal_id) {
        if (!Network.checkNetwork(BaseContext)) {
            ToastUtil.makeText(BaseContext, networkFail).show();
            return;
        }
        mView.onRequestStart();
        ApplyStatusBean bean = new ApplyStatusBean();
        bean.deal_id = deal_id;
        Api.api_service.getApplyStatus(bean).compose(RxLifeUtil.checkOn(BaseContext)).
                doFinally(() -> {
                    try {
                        mView.complete();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    mView.getApplyForSuccess(result);
                });
    }

}
